import 'dart:ui';

import 'package:video_chewie_mutant/src/chewie_player.dart';
import 'package:video_chewie_mutant/src/cupertino_controls.dart';
import 'package:video_chewie_mutant/src/material_controls.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'dart:async';

class PlayerWithControls extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PlayerWithControlsState();
  }
}

class PlayerWithControlsState extends State<PlayerWithControls> {
  double videoHeightRatio = 1.0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    ChewieController?.of(context)?.addListener(_listener);
  }

  void _listener() {
    final ChewieController chewieController = ChewieController.of(context);
    if (chewieController != null && chewieController.marchTarget != videoHeightRatio) {
      videoHeightRatio = chewieController.marchTarget;
      Future.delayed(Duration(milliseconds: 16), () {
        setState(() {});
      });
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void deactivate() {
    if (mounted) {
      ChewieController?.of(context)?.removeListener(_listener);
    }
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    final ChewieController chewieController = ChewieController.of(context);
    final videoSize = chewieController.videoPlayerController.value.size;
    double _calculateAspectRatio(BuildContext context) {
      final size = MediaQuery.of(context).size;
      final width = size.width;
      final height = size.height;

      return width > height ? width / height : height / width;
    }

    Widget _buildControls(
      BuildContext context,
      ChewieController chewieController,
    ) {
      final controls = Theme.of(context).platform == TargetPlatform.android
          ? const MaterialControls()
          : const CupertinoControls(
              backgroundColor: Color.fromRGBO(41, 41, 41, 0.7),
              iconColor: Color.fromARGB(255, 200, 200, 200),
            );
      return chewieController.showControls ? chewieController.customControls ?? controls : Container();
    }

    Stack _buildPlayerWithControls(ChewieController chewieController, BuildContext context) {
      return Stack(
        children: <Widget>[
          chewieController.placeholder ?? Container(),
          if (chewieController.changeTargetRatio == null)
            Center(
              child: AspectRatio(
                aspectRatio: chewieController.aspectRatio ?? chewieController.videoPlayerController.value.aspectRatio,
                child: VideoPlayer(chewieController.videoPlayerController),
              ),
            )
          else
            AnimatedContainer(
              height: MediaQuery.of(context).size.height * videoHeightRatio,
              duration: Duration(milliseconds: chewieController.videoTransformDuration),
              alignment: Alignment.center,
              child: AspectRatio(
                aspectRatio: chewieController.aspectRatio ?? chewieController.videoPlayerController.value.aspectRatio,
                child: VideoPlayer(chewieController.videoPlayerController),
              ),
            ),

          //control panel
          if (!chewieController.isFullScreen)
            _buildControls(context, chewieController)
          else
            Container(
              margin: EdgeInsets.symmetric(horizontal: videoSize.width > videoSize.height ? 50 : 0),
              child: _buildControls(context, chewieController),
            ),
          //overlay
          if (chewieController.overlay != null && !chewieController.isFullScreen) chewieController.overlay,

          if (chewieController.rotatedOverlay != null && chewieController.isFullScreen && videoSize.width > videoSize.height)
            chewieController.rotatedOverlay,

          if (chewieController.overlay != null && chewieController.isFullScreen && videoSize.width < videoSize.height) chewieController.overlay,
        ],
      );
    }

    return Container(
      color: Colors.black,
      height: MediaQuery.of(context).size.height,
      child: AspectRatio(
        aspectRatio: _calculateAspectRatio(context),
        child: _buildPlayerWithControls(chewieController, context),
      ),
    );
  }
}
